package com.lfk.demo.Ability;

import com.lfk.justwetools.Utils.Log;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;

import com.lfk.justwetools.Component.PaintIt.OnPathListener;
import com.lfk.justwetools.Component.PaintIt.PaintView;
import com.lfk.justwetools.Component.PaintIt.PathNode;
import com.lfk.justwetools.Component.PaintIt.UserInfo;
import com.lfk.demo.ResourceTable;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.multimodalinput.event.KeyEvent;
import ohos.utils.IntentConstants;

import java.io.File;

public class PaintViewAbility extends Ability implements Component.ClickedListener,Component.LongClickedListener {
    private PaintView paintView;
    private PathNode pathNode;
    private TextField editText;
    private static String PATH;
    private File dir;
    private CommonDialog alertDialog;
    private CommonDialog alertDialog2;
    private static final int SELECT_PICTURE = 2;
    private static final int SELECT_FILE = 3;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setPattern(ResourceTable.Pattern_base);
        setUIContent(ResourceTable.Layout_ability_paint_view);

        PATH = getExternalFilesDir(null).getPath() + "/DrawAPicture";

        // Just a line ,you can use a Paintview.
        paintView = (PaintView) findComponentById(ResourceTable.Id_paintView);
        // If you want to have more function , you have to do this.
        // 1.new a PathNode (Don't forget add "ohos:name=".PaintIt.PathNode" in
        // config.json)
        pathNode = (PathNode) getAbilityPackage();

        // 2.allow record

        paintView.setIsRecordPath(true, pathNode);

        // 3.setOnPathClickListener add your every touch message

        paintView.setOnPathListener(new OnPathListener() {
            @Override
            public void addNodeToPath(float x, float y, int event, boolean IsPaint) {
                PathNode.Node tempnode = pathNode.new Node();
                // convert px to dp
                tempnode.x = paintView.px2dip(x);
                tempnode.y = paintView.px2dip(y);
                if (IsPaint) {
                    tempnode.PenColor = UserInfo.PaintColor;
                    tempnode.PenWidth = UserInfo.PaintWidth;
                } else {
                    tempnode.EraserWidth = UserInfo.EraserWidth;
                }
                tempnode.IsPaint = IsPaint;
                Log.e(tempnode.PenColor + ":" + tempnode.PenWidth + ":" + tempnode.EraserWidth, tempnode.IsPaint + "");
                tempnode.TouchEvent = event;
                tempnode.time = System.currentTimeMillis();
                pathNode.addNode(tempnode);
            }
        });

        // 4.set buttons
        findComponentById(ResourceTable.Id_save).setClickedListener(this);
        findComponentById(ResourceTable.Id_redraw).setClickedListener(this);
        findComponentById(ResourceTable.Id_paint).setClickedListener(this);
        findComponentById(ResourceTable.Id_eraser).setClickedListener(this);
        findComponentById(ResourceTable.Id_clear).setClickedListener(this);
        findComponentById(ResourceTable.Id_file).setClickedListener(this);
        findComponentById(ResourceTable.Id_color).setClickedListener(this);
        findComponentById(ResourceTable.Id_file).setLongClickedListener(this);
        findComponentById(ResourceTable.Id_paint).setLongClickedListener(this);
        findComponentById(ResourceTable.Id_eraser).setLongClickedListener(this);
        findComponentById(ResourceTable.Id_save).setLongClickedListener(this);


        // 5.new a folder
        save();
    }

    private void save() {
        dir = new File(PATH);
        if (!dir.exists()) {
            dir.mkdirs();
            Log.e(PATH, dir.mkdirs() + "");
        }
    }

    @Override
    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_save:
                save();
                paintView.BitmapToPicture(dir);
                break;
            case ResourceTable.Id_clear:
                // You should clean 3 (Paintview/list to record/list to build Redo/Undo)
                if (!paintView.isShowing()) {
                    paintView.clean();
                    pathNode.clearList();
                    paintView.clearReUnList();
                }
                break;
            case ResourceTable.Id_redraw:
                paintView.preview(pathNode.getPathList());
                break;
            case ResourceTable.Id_eraser:
                // use eraser
                paintView.Eraser();
                break;
            case ResourceTable.Id_paint:
                // use paint
                paintView.Paint();
                break;
            case ResourceTable.Id_file:
                // input a picture
                Intent intent = new Intent();
                intent.setType("image/*");
                startAbilityForResult(Intent.makeMainAbilityPicker(IntentConstants.ACTION_CHOOSE, "选择图片"), SELECT_PICTURE);
                break;
            case ResourceTable.Id_color:
                // Now you could use ColorPickerView
                // uri：https://code.google.com/p/color-picker-view/
                // then
                paintView.setColor(0xae33feff);
                break;
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            switch (requestCode) {
                case SELECT_PICTURE:
                    paintView.setmBitmap(data.getUri());
                    break;
                case SELECT_FILE:
                    paintView.JsonToPathNodeToHandle(data.getUri());
                    break;
            }

        }
    }

    @Override
    public void onLongClicked(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_file:
                // input a dir with ".lfk"
                Intent intent = new Intent();
                intent.setType("file/*");
                intent.setAction(IntentConstants.ACTION_CHOOSE);
                startAbilityForResult(intent, SELECT_FILE);
                break;
            case ResourceTable.Id_save:
                save();
                // save to .lfk  frame-by-frame animation (through json)
                paintView.PathNodeToJson(pathNode, dir);
                break;
            case ResourceTable.Id_paint:
                // set PaintWidth
                editText = new TextField(this);
                alertDialog = new CommonDialog(this).setTitleText("请输入0 - 100之间的数字")
                        .setContentCustomComponent(editText)
                        .setButton(IDialog.BUTTON2, "确定", new IDialog.ClickedListener(){
                            @Override
                            public void onClick(IDialog iDialog, int which) {
                                // set in UserInfo
                                UserInfo.PaintWidth = Integer.parseInt(editText.getText().toString());
                                paintView.setPenWidth(Integer.parseInt(editText.getText().toString()));
                                alertDialog.destroy();
                            }
                        })
                        .setButton(IDialog.BUTTON3, "取消", new IDialog.ClickedListener(){
                            @Override
                            public void onClick(IDialog iDialog, int which) {
                                alertDialog.destroy();
                            }
                        });
                alertDialog.show();
                break;
            case ResourceTable.Id_eraser:
                // set EraserWidth
                editText = new TextField(this);
                alertDialog2 = new CommonDialog(this).setTitleText("请输入0 - 100之间的数字")
                        .setContentCustomComponent(editText)
                        .setButton(IDialog.BUTTON2, "确定", new IDialog.ClickedListener(){
                            @Override
                            public void onClick(IDialog iDialog, int which) {
                                // set in UserInfo
                                UserInfo.EraserWidth = Integer.parseInt(editText.getText().toString());
                                paintView.setmEraserPaint(Integer.parseInt(editText.getText().toString()));
                                alertDialog2.destroy();
                            }
                        })
                        .setButton(IDialog.BUTTON3, "取消", new IDialog.ClickedListener(){
                            @Override
                            public void onClick(IDialog iDialog, int which) {
                                alertDialog2.destroy();
                            }
                        });
                alertDialog.show();
                break;

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEY_VOLUME_UP:
                // redo
                paintView.ReDoORUndo(true);
                return true;
            case KeyEvent.KEY_VOLUME_DOWN:
                // undo
                paintView.ReDoORUndo(false);
                return true;
        }
        return false;
    }


}
